

#====================================================================
#
#		.NET IMAGING FUNCTIONS
#
#====================================================================


#--------------------------------------------------------------------
#~ Calculates the aspect-ratio (width/height) of a rectangle.
#~ Returns the calcuated aspect ratio as a double.
Function Calculate-AspectRatio
{
	param
	(
		#~ The width of the rectangle.
		  [int]				$Width				= $( throw "Width is a required parameter." )
		#~ The height of the rectangle.
		, [int]				$Height				= $( throw "Height is a required parameter." )
	)
	if( $Height -eq 0 )
		{ return 0 }
	else
		{ return ($Width / $Height) }
}


#--------------------------------------------------------------------
Function Create-Image
{
	param
	(
		  [int]				$Width				= $( throw "Width is a required parameter." )
		, [int]				$Height				= $( throw "Height is a required parameter." )
		, [System.Drawing.Imaging.PixelFormat]
							$PixelFormat		= [System.Drawing.Imaging.PixelFormat]::Format24bppRgb
	)
	trap
	{
		Write-Error "`nError - $_"
		return $null
	}
 	return New-Object System.Drawing.Bitmap( $Width, $Height, $PixelFormat )
}


#--------------------------------------------------------------------
Function Get-Image
{
	param
	(
		  [string]			$Path				= $( throw "Path is a required parameter." )
	)
	trap
	{
		Write-Error "`nError - $_"
		return $null
	}
 	return [System.Drawing.Image]::FromFile( $Path )
}


#--------------------------------------------------------------------
Function Size-Image
{
	param
	(
		  [System.Drawing.Image]
		  					$Image				= $( throw "Image is a required parameter." )
		, [int]				$Width				= 0
		, [int]				$Height				= 0
		, [bool]			$StretchAspect		= $false
	)
	trap
	{
		Write-Error "`nError - $_"
		return $null
	}
	if( ($Width -gt 0) -or ($Height -gt 0) )
	{
		[float] $AspectRatio = Calculate-AspectRatio $Image.Width $Image.Height
		if( $Width -le 0 )
		{
			# Calculate the Width.
			$Width = ($Height * $AspectRatio)
		}
		elseif( $Height -le 0 )
		{
			# Calculate the Height.
			$Height = ($Width / $AspectRatio)
		}
		elseif( -not $StretchAspect )
		{
			[float] $StretchedAspectRatio = Calculate-AspectRatio $Width $Height
			if( $AspectRatio -gt $StretchedAspectRatio )
			{
				# Fit the Height
 				$Height = ($Image.Height * ($Width / $Image.Width))
			}
			else
			{
				# Fit the Width
				$Width = ($Image.Width * ($Height / $Image.Height))
			}
		}
		# Resize the image.
		$Image = $Image.GetThumbnailImage( $Width, $Height, $null, [System.IntPtr]::Zero )
	}
	return $Image
}


#--------------------------------------------------------------------
Function Serialize-Image
{
	param
	(
		  [System.Drawing.Image]
		  					$Image				= $( throw "Image is a required parameter." )
		, [System.Drawing.Imaging.ImageFormat]
							$ImageFormat		= [System.Drawing.Imaging.ImageFormat]::Png
	)
	trap
	{
		Write-Error "`nError - $_"
		return $null
	}
	[System.IO.MemoryStream] $Stream = New-Object System.IO.MemoryStream
	$Image.Save( $Stream, $ImageFormat )
	[byte[]] $Bytes = $Stream.ToArray()
	$Stream.Close()
	return $Bytes
}

